package com.jxl.springboot.game.controller;

import com.jxl.springboot.game.pojo.User;
import com.jxl.springboot.game.service.UserService;
import com.jxl.springboot.game.utils.CookieUtils;
import com.jxl.springboot.game.utils.RedisUtils;
import com.jxl.springboot.game.utils.RandomUtils;
import com.jxl.springboot.game.web.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 用户控制器
 *
 * @author 江雪林
 * @create 2021-03-29
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    RedisUtils redisUtils;

//    @RequestMapping(value = "/token",method = RequestMethod.POST)
//    @ResponseBody
//    public WebResult getToken(@RequestParam("username") String username,
//                              @RequestParam("password") String password,
//                              Map<String,Object> map) throws IOException {
//        WebResult webResult = new WebResult();
//        if ((!"".equals(username)) && (!"".equals(password))) {
//            int result = userService.login(username, password);
//            if (result>0) {
//                String token = StringUtils.randomStr();
//                String uid = Constant.U_ID+result;
//                //存储在redis 缓存中
//                redisUtils.set(uid,token,Constant.SENDS);
//                webResult.put(Constant.U_ID,uid).put(Constant.R_ID,token);
//                webResult.setMessage(Constant.LOGIN_SUCCESS);
//            }else {
//                webResult.setMessage(Constant.LOGIN_UNSUCCESS);
//            }
//        }
//        return webResult;
//    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String userLogin(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            Map<String,Object> map,HttpServletResponse resp) throws IOException {
        if ((!"".equals(username)) && (!"".equals(password))) {
            int result = userService.login(username, password);
            if (result>0) {
                String token = RandomUtils.randomStr();
                String uid = Constant.U_ID+result;
                //存储在redis 缓存中
                redisUtils.set(uid,token,Constant.SENDS);
                CookieUtils.addCookie(resp,Constant.U_ID, uid,Constant.SENDS);
                CookieUtils.addCookie(resp,Constant.R_ID, token,Constant.SENDS);
                return "redirect:/main";
            }
        }
        map.put("msg",Constant.LOGIN_UNSUCCESS);
        return "/login";
    }

    @RequestMapping("loginOut")
    public String loginOut(HttpServletResponse resp,HttpServletRequest req) {
        String uid = CookieUtils.getCookieValueByName(req, Constant.U_ID);
        String rid = CookieUtils.getCookieValueByName(req, Constant.R_ID);
        if(uid!=null || "".equals(uid)){
            redisUtils.del(uid);
        }
        if(rid!=null || "".equals(rid)){
            redisUtils.del(rid);
        }
        return "redirect:/login";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(HttpServletResponse resp, User user) throws IOException {
        if (user != null && user.getPassword() != "" & user.getUsername() != "") {
            if (userService.register(user)) {
                return "redirect:/login";
            }
        }
        return "register";
    }
}
